package com.csdj.springboottest.service;

import com.csdj.springboottest.entity.User;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.transform.Templates;
import java.util.concurrent.TimeUnit;

/**
 * author:cloud
 * date:2021/4/21 0021
 **/
@Service
public class RedisService {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private ValueOperations<String,Object> valueOperations;
    @Resource
    private HashOperations<String,String,Object> hashOperations;

    public void set(String key, Object data,int second) {
        valueOperations.set(key, data, second, TimeUnit.SECONDS);
    }

    public void setHash(String key,String field,Object data){
        hashOperations.put(key,field,data);
    }

    public Object get(String key){
        return valueOperations.get(key);
    }

    public void publish(String topic,String msg){
        redisTemplate.convertAndSend(topic,msg);
    }

    public boolean delKey(String key){
        redisTemplate.boundValueOps(key).get();
        return redisTemplate.delete(key);
    }

    public String getString(String key){
        Object object = valueOperations.get(key);
        if(object != null)return object.toString();
        return null;
    }

}
